package com.bawei.club.test.recyclerview

import android.os.Bundle
import androidx.recyclerview.widget.DiffUtil

/**
 * @author yaotianxue
 * @description
 * @date 2025/5/30
 */
class MyDiffCallBack(val oldData: List<MyModel>, val newData: List<MyModel>): DiffUtil.Callback() {
    override fun getOldListSize(): Int {
        return oldData.size
    }

    override fun getNewListSize(): Int {
        return newData.size
    }

    /**
     * 判断是否同一个对象
     */
    override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        return oldData[oldItemPosition].id == newData[newItemPosition].id
    }

    /**
     * 判断内容是否相同
     */
    override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        return oldData[oldItemPosition] == newData[newItemPosition]
    }

    /**
     * 局部增量刷新：是同一个对象，但是内容不同
     */
    override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? {
        val old = oldData[oldItemPosition]
        val new = newData[newItemPosition]

        if (old.isLike != new.isLike) {
            return "like"
        }
        if (old.name != new.name){
            return "name"
        }
        if (old.url != new.url){
            return "url"
        }
        return null
    }
}